package edu.android.proyecto;

import java.io.IOException;

import edu.android.proyecto.sqliteclient.Database;
import edu.android.proyecto.sqliteclient.UsuarioDbHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity{

    EditText etUsuario = null;
    EditText etPassword = null;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        try {
			Database.copyDataBase(this);
		} catch (IOException e) {
			e.printStackTrace();
		}
        etUsuario = (EditText) findViewById(R.id.txtUsuario);
        etPassword = (EditText) findViewById(R.id.txtPassword);
        
        Button btnIngresar = (Button) findViewById(R.id.btnIngresar);

        btnIngresar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
		        System.out.println(etUsuario.getText()+"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"+etPassword.getText());
				if (valida()) {
					Intent intent = new Intent(LoginActivity.this,ListMesaActivity.class);
					startActivity(intent);
				}else {
					MuestraAlert("El usuario o password son incorrectos!");
				}
			}
		});
    }
	
    public void MuestraAlert(String mensaje){
    	
    	AlertDialog.Builder alerta = new AlertDialog.Builder(this);
    	alerta.setMessage(mensaje);
    	
    	alerta.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
    	alerta.show();
    }
    
	 public boolean valida(){
		boolean usu = new UsuarioDbHelper(this).ValidaLogin(etUsuario.getText()+"", etPassword.getText()+"");
		return usu;
	 }
}
